﻿using AutoMapper;
using ERPSystem.Common.Domains.WarehouseCommon;
using ERPSystem.Common.DTO;
using ERPSystem.Common.Infrastructure.IRepository;
using ERPSystem.Enum.CommonErrorCodes;
using MediatR;

namespace BasicInformationWrite.API.BasicInformationCommons.Warehousees
{
    public class WarehouseCreateHandler : IRequestHandler<WarehouseCreateCommand, Result>
    {
        private readonly IBaseRepository<Warehouse> _warehouseRepository;
        private readonly IMapper _mapper;

        public WarehouseCreateHandler(IBaseRepository<Warehouse> warehouseRepository, IMapper mapper)
        {
            _warehouseRepository = warehouseRepository;
            _mapper = mapper;
        }
        /// <summary>
        /// 新增仓库业务逻辑
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result> Handle(WarehouseCreateCommand request, CancellationToken cancellationToken)
        {
            var result = new Result();
            try
            {
                Warehouse warehouse = _mapper.Map<Warehouse>(request);
                warehouse.CreatedOwnerId = "张三";
                warehouse.CreatedTime = DateTime.Now;
                var res = await _warehouseRepository.Add(warehouse);
                if (res > 0)
                {
                    result.Code = (int)CommonErrorCode.Success;
                    result.Msg = "添加成功";
                }
                else
                {
                    result.Msg = "新增失败";
                }
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {

            }
            return result;
        }
    }
}
